package com.cl.unipay4j.wx;

import com.cl.unipay4j.wx.pay.WxTradeTypeEnum;
import com.cl.unipay4j.wx.sdk.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信配置实现类
 * @author  sias_han
 */
public class WxPayConfigImpl extends IWXPayConfig {

    private static WxPayConfigImpl INSTANCE;

    private WxPayConfig context;
    private WxTradeTypeEnum tradeType = WxTradeTypeEnum.JSAPI;


    private WxPayConfigImpl() {

    }

    public static WxPayConfigImpl getInstance() {
        // 此处单例模式存在问题
//        if (INSTANCE == null) {
//            synchronized (WxPayConfigImpl.class) {
//                if (INSTANCE == null) {
//                    INSTANCE = new WxPayConfigImpl();
//                }
//            }
//        }
//        return INSTANCE;
        return new WxPayConfigImpl();
    }



    @Override
    public  WXPayConstants.SignType getSignType(){
        String signType = context.getSignType();
        if (WXPayConstants.MD5.equals(signType)){
            return WXPayConstants.SignType.MD5;
        }else if (WXPayConstants.HMACSHA256.equals(signType)){
            return WXPayConstants.SignType.HMACSHA256;
        }
        return WXPayConstants.SignType.MD5;
    }

    @Override
    public String getAppID() {
        String appId = null;
        switch (tradeType) {
            case H5:
            case JSAPI:
            case NATIVE:
            case MICROPAY:
                appId = context.getAppid();
                break;
            case APP:
                appId = context.getAppAppId();
                break;
            case XCX:
                appId = context.getAppletAppid();
                break;
            default:
                appId = context.getAppid();
                break;
        }
        return appId;
    }

    @Override
    public String getMchID() {
        return context.getMchid();
    }

    @Override
    public String getKey() {
        String key;
        if (context.isUseSandbox()) {
            key = getSandboxSignKey();
        } else {
            key = context.getKey();
        }
        return key;
    }

    @Override
    public InputStream getCertStream() {
        WxPayConfig context = this.context;
        InputStream inputStream = WxPayConfigImpl.class.getClassLoader().getResourceAsStream(context.getCertPath());
        return inputStream;
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }

    @Override
    public boolean shouldAutoReport() {
        return false;
    }

    public WxPayConfig getContext() {
        return context;
    }

    public void setContext(WxPayConfig context) {
        this.context = context;
    }

    public WxTradeTypeEnum getTradeType() {
        return tradeType;
    }

    public void setTradeType(WxTradeTypeEnum tradeType) {
        this.tradeType = tradeType;
    }

    public String getSandboxSignKey() {
        try {
            WXPay wxPay = new WXPay(this);
            Map<String, String> params = new HashMap<String, String>();
            params.put("mch_id", this.getMchID());
            params.put("nonce_str", WXPayUtil.generateNonceStr());
            params.put("sign", WXPayUtil.generateSignature(params, this.context.getKey()));
            String strXML = wxPay.requestWithoutCert("/sandboxnew/pay/getsignkey",
                    params, this.getHttpConnectTimeoutMs(), this.getHttpReadTimeoutMs());

            Map<String, String> result = WXPayUtil.xmlToMap(strXML);
            if (WXPayConstants.SUCCESS.equals(result.get(WXPayConstants.RETURN_CODE))) {
                return result.get("sandbox_signkey");
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }


}
